package com.zqxq.lottery.controller.news;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.entity.o2o.news.MatchNewsLikesO2O;
import com.zqxq.lottery.entity.o2o.news.MatchNewsO2O;
import com.zqxq.lottery.entity.po.news.MatchNewsLikesPO;
import com.zqxq.lottery.entity.po.news.MatchNewsPO;
import com.zqxq.lottery.entity.po.user.UserInfoPO;
import com.zqxq.lottery.entity.vo.jczq.JczqMatchLiveVO;
import com.zqxq.lottery.entity.vo.news.MatchNewsLikesVO;
import com.zqxq.lottery.entity.vo.news.MatchNewsVO;
import com.zqxq.lottery.mapper.news.MatchNewsLikesMapper;
import com.zqxq.lottery.service.jczq.IJczqMatchService;
import com.zqxq.lottery.service.news.IMatchNewsService;
import com.zqxq.lottery.service.user.IUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 苦智
 * @date 2018/9/5 10:31
 * @description 资讯controller
 * @ClassName MatchNewsController
 */
@RestController
@Slf4j
@RequestMapping("/matchNews")
public class MatchNewsController {
    @Autowired
    private IMatchNewsService matchNewsService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private MatchNewsLikesMapper matchNewsLikesMapper;
    @Autowired
    private IJczqMatchService matchService;

    @PostMapping("/home")
    public Result getHomeNews(Integer type,Integer pageNo,Integer pageSize){
        if (null == type)
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        Page<MatchNewsVO> page = matchNewsService.selectPageByType(type,pageNo,pageSize);
        return new Result(CodeEnum.SUCCESS.getCode(),"成功",page);
    }

    @PostMapping("/click")
    public Result clickNews(String newsId){
        MatchNewsPO po = matchNewsService.selectById(newsId);
        if (po == null)
            return new Result(CodeEnum.FAIL_100002.getCode(),"资讯不存在");
        //保存点击量
        Integer nums = po.getClick_nums();
        nums+=1;
        po.setClick_nums(nums);
        matchNewsService.updateById(po);
        return new Result(CodeEnum.SUCCESS.getCode(),"成功",po.getClick_nums());
    }

    @PostMapping("/detail")
    public Result getDetailNews(String newsId){
        if (StrUtils.isEmpty(newsId)){
            log.info("资讯id不能为空");
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        }
        MatchNewsPO po = matchNewsService.selectById(newsId);
        MatchNewsVO matchNewsVO = new MatchNewsVO();
        if (po != null){
            matchNewsVO = MatchNewsO2O.toVO(po);
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("news_id",newsId);
            wrapper.eq("status",2);
            Integer count = matchNewsLikesMapper.selectCount(wrapper);
            matchNewsVO.setLikesNum(count);
        }

        return new Result(CodeEnum.SUCCESS.getCode(),"成功", matchNewsVO);
    }

    //用户点赞资讯
    @PostMapping("/like")
    public Result likeDetailNews(String newsId,String userId,String payAmount){
        if (StrUtils.isEmpty(newsId) || StrUtils.isEmpty(userId)){
            log.info("资讯id或用户id不能为空");
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        }
        MatchNewsPO po = matchNewsService.selectById(newsId);
        UserInfoPO userInfoPO = userInfoService.selectById(userId);
        if (po == null || userInfoPO == null){
            return new Result(CodeEnum.FAIL.getCode(),"用户或资讯不存在");
        }
        MatchNewsLikesPO matchNewsLikesPO = new MatchNewsLikesPO();
        matchNewsLikesPO.setNews_id(Long.valueOf(newsId));
        matchNewsLikesPO.setUser_id(Long.valueOf(userId));
//        matchNewsLikesPO.setStatus(1);
        MatchNewsLikesPO likesPO = matchNewsLikesMapper.selectOne(matchNewsLikesPO);
        if (likesPO != null){
            return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),MatchNewsLikesO2O.toVO(likesPO));
        }
        likesPO = matchNewsService.saveNewsLikes(newsId,userId,payAmount);
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(), MatchNewsLikesO2O.toVO(likesPO));
    }

    /**
    * @Author 苦智
    * @Description 打赏支付
    * @Date 12:20 2018/10/16
    * @Param []
    * @return com.zqxq.common.constant.Result
    **/
    @PostMapping("payReward")
    public Result payReward(@RequestBody MatchNewsLikesVO vo){
        if (vo == null)
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        if (StrUtils.isEmpty(vo.getLikesId()))
            return new Result(CodeEnum.FAIL_100002.getCode(),"订单id为空");
        return matchNewsService.payReward(vo);
    }


    //资讯首页赛事列表
    @PostMapping("/match")
    public Result getHomeMatch(){
        List<JczqMatchLiveVO> list = matchService.getNewsHomeMatch();
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),list);
    }

}
